<div class="layui-hide" id='enian-nav-bar'>
 	<a data-url="">系统日志</a>
</div>

<div class="layui-btn-group enian-btn-group">
  	<button id='search' class="layui-btn layui-btn-sm layui-btn-primary">
  		<i class="layui-icon">&#xe615;</i><div class="enian-btn-group-text">查询/筛选</div>
  	</button>
  	<button id='del' class="layui-btn layui-btn-sm layui-btn-primary">
  		<i class="layui-icon">&#xe640;</i><div class="enian-btn-group-text">删除</div>
  	</button>
</div>
<input type="text" name="del-laydate" id="del-laydate" class="layui-hide" value="" />
<div id="data"></div>
<script type="text/html" id="tpl-status">
  {{# if(d.status == 1){  }}
  		<div style="color: forestgreen;">成功</div>
  {{# }else{ }}
  		<div style="color: orangered;">失败</div>
  {{# } }}
</script>

<script type="text/html" id="tpl-type">
  {{# if(d.type == 1){  }}
  		<div style="color: ;">用户登录</div>
  {{# }else if(d.type == 2){  }}
  		<div style="color: ;">用户操作</div>
  {{# }else if(d.type == 3){  }}
  		<div style="color: ;">权限管理</div>
  {{# }else{ }}
  		<div style="color: ;">应用操作</div>
  {{# } }}
</script>


<script>
layui.use(['jquery','enianTable','laydate','layer'], function() {
	var table = layui.table
	,$ = layui.jquery
	,enianTable=layui.enianTable
	,layer=layui.layer
	,laydate = layui.laydate;
	var tableArg={where:[],order:[]};
	//年月范围选择
	laydate.render({ 
	  elem: '#del-laydate'
	  ,type: 'month'
	  ,eventElem: '#del'
	  ,trigger: 'click'
	  ,range: true //或 range: '~' 来自定义分割字符
	  ,done:function(value, date, endDate){
	  	//console.log(value); //得到日期生成的值，如：2017-08-18
	  	layer.confirm('您确定删除 '+value+' 这段时间的日志吗，删除后无法恢复也要这样做吗？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			// console.log('确定')
			  var d={start:date.year+'-'+date.month,end:endDate.year+'-'+endDate.month};
			  $.ajax({
			  	type:"post",
			  	url:"{:url('',['t'=>2])}",
			  	data:{time:d},
			  	async:true,
			  	success:function(r){
			  		if(r.code==1){
			  			layer.msg("操作成功，请手动刷新。本次操作共删除记录："+r.count);
			  		}else{
			  			layer.alert('操作失败');
			  		}
			  	}
			  });
		});
    	// console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
    	// console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
	  }
	});
	
	$('#SadminList_add').click(function(){
		enianAdmin.go('{:url("addAdmin")}')
    });
 	var fieldData = [
 	{"tableHead":{field:'id', width:60, title: 'ID',sort: true},type:'noInput',notRender:['search']}
 	,{"tableHead":{field:'aid', width:130, title: '用户id'},type:'noInput',notRender:['search']}
 	,{"tableHead":{field:'type',width:100, title: '类型',templet:'#tpl-type'},type:'noInput',notRender:['search']}
 	,{"tableHead":{field:'status',title: '状态'},type:'switch',notRender:['table'],typeData:{on:{data:1,name:"成功"},off:{data:0,name:"失败"}}}//渲染搜索框
 	,{"tableHead":{field:'status', width:100,title: '状态', templet:'#tpl-status'},type:'input',notRender:['search'],typeData:{msg:''}}//渲染表格
 	,{"tableHead":{field:'content', title: '详情'},type:'noInput',typeData:{msg:'箭头前关键字需要用英文'},notRender:['form']}
 	,{"tableHead":{field:'time', width:170,title: '时间',sort: true},type:'time',typeData:{msg1:'开始时间',msg2:'结束时间'}}
 	,{"tableHead":{field:'ip', width:150, title: 'IP'},type:'noInput',notRender:['search']}
 	]
 	
 	
	layui.link('/static/layui_extends/enianTable/enianTable.css');
    var ets = enianTable.search(fieldData);
    $('#search').click(function(){
		ets.open();
    });
    
    //监听搜索
	ets.submit(function(d){
		// console.log(d);
		getTable.renderObj().reload({
		  where:{where:d}
		  ,page: {
		    curr: 1 //重新从第 1 页开始
		  }
		});
	});
	
	var getTable = enianTable.table({
		elem:"#data"
		,url:"{:url('',['t'=>1])}"
		,data:fieldData
		,config:{
			limit:30
			,height:evar.tableHeight
			,skin:'nob'
			,size:'sm'
			,toolbar:true
			,page:true
			,autoSort: false
			,method:'post'
			//详见layui文档 数据表格 - 基本参数
		}
	})
	
	//监听排序
	getTable.on('sort',function(obj){
		// console.log(obj.field); //当前排序的字段名
		// console.log(obj.type); //当前排序类型：desc（降序）、asc（升序）、null（空对象，默认排序）
		//console.log(this); //当前排序的 th 对象
		tableArg.order= obj.field + " " + obj.type;
		// console.log(tableArg);
		table.reload(getTable.id, {
		    initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
		    ,where: tableArg
		});
	})
});
</script>
